/**
 * Copyright (C), 2015-2020, XXX有限公司
 * FileName: MyMVCConfig
 * Author:   吴建森
 * Date:     2020/12/6 15:35
 * Description:
 * History:
 * <author>吴建森      <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.sen.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * 〈一句话功能简述〉<br>
 * 〈配置mvc相关配置例如跨域〉
 *
 * @author 吴建森
 * @create 2020/12/6
 * @since 1.0.0
 */
@Configuration
public class MyMVCConfig implements WebMvcConfigurer {

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        // 允许访问的客户端域名
        List<String> allowedOriginPatterns = new ArrayList<>();
        allowedOriginPatterns.add("http://localhost:9527");
        config.setAllowedOriginPatterns(allowedOriginPatterns);
//        config.addAllowedOrigin(serverPort);
        // 允许服务端访问的客户端请求头
        config.addAllowedHeader("*");
        // 允许访问的方法名,GET POST等
        config.addAllowedMethod("*");
        // 对接口配置跨域设置
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
}
   /* @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                //允许不同的域访问
                .allowedOrigins("localhost:9527")
//                身份验证
                .allowCredentials(true)
//                允许不同的请求方式
                .allowedMethods("HEAD", "OPTIONS", "GET", "POST", "PUT", "PATCH", "DELETE")
//                允许各种请求头
                .allowedHeaders("*")
                .maxAge(3600);
    }
}*/
